var app = getApp();
const ctx = wx.createCanvasContext('visitingCardCanvas')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth:'',
    canHeight:'',
    winWidth1:'',
    region: ['', '', ''],
    switch1: false,
    switch2: true,
    switch3: true,
    imageList: '',
    srcs: ['', '', '', ''],
    flag0: false,
    flag1: false,
    flag2: false,
    flag3: false,
    flag4: false,
    selectPerson: true,
    content: '',
    selectArea: false,
    types: [],
    first:false,
    host: app.d.hostImg,
    type_id:0,
    introduce:0,
    sellAdmin:0,
    username:'',
    seller:'',
    shareimg:'',
    modelimg:'',
    store:'',
    url:'',
    order_num:0,
    pro_num:0,
    user_num:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
onLoad: function (options) {
    var that=this
    // if (app.d.userId){
    //   this.getStatus()
    //   this.getTypes()
    // }else{
      wx.showToast({
        title: '加载数据中',
        icon:'loading'
      })
      this.setData({
        switch2: true,
        switch1: true 
      })
      setTimeout(function(){
        that.getStatus()
        that.setData({
            first: true
          })
      },2000)
},
getStatus:function(){
  var that=this
  wx.request({
    url: app.d.ceshiUrl + '/Api/BIndex/recruitment_index',
    method: 'post',
    data: {
      user_id: app.d.userId
    },
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      console.log(res.data.status);
      var content=res.data.content
      if (res.data.status == 2) {//待审核
        that.setData({
          switch2: false,
          switch1: true
        })
      } else if (res.data.status == 0){//申请
        that.setData({
          switch2: true,
          switch1: false,
        })
        that.getTypes();
      }else if (res.data.status == 3){//审核通过
        that.setData({
          switch2: true,
          switch1: true,
          store:res.data.store,
          order_num:res.data.order_num,
          pro_num:res.data.pro_num,
          user_num:res.data.user_num,
        })
        app.globalData.platform=false;
        that.initNavHeight();
        that.fenxiang();
      }else if (res.data.status == 4){//审核未通过
        that.setData({
          switch2: true,
          switch1: true,
          switch3: false,
          seller:content,
          type_id:content.type_id,
          'srcs[0]':content.card_img,
          'srcs[1]':content.card_img1,
          'srcs[2]':content.brand_logo,
          'srcs[3]':content.permit_img,
          'srcs[4]':content.phy_img,
          flag0:true,
          flag1:true,
          flag2:true,
          flag3:true,
          flag4:true,
          'region[0]':content.sheng,
          'region[1]':content.city,
          'region[2]':content.quyu,
          introduce:content.introduce,
        })
         that.getTypes();
        console.log(that.data.srcs)
      }else{
        that.setData({
          switch2: true,
          switch1: false
        })
        that.getTypes();
      }
    },
    fail: function (e) {
      wx.showToast({
        title: '网络不给力！',
        duration: 2000
      });
    },
  });
},
getTypes:function(){
  var that = this
  wx.request({
    url: app.d.ceshiUrl + '/Api/BIndex/sccat',
    method: 'post',
    data: {
    },
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      var ad = res.data.ad;
      that.setData({
        types: ad
      });

    },
    fail: function (e) {
      wx.showToast({
        title: '网络不给力！',
        duration: 2000
      });
    },
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
onReady: function () {


},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.froms = 'company_user'
    console.log(app.globalData.froms)
    var that=this
    if(this.data.first){
      if (app.d.userId) {
        this.getStatus()
        // this.getTypes()
      } else {
        wx.showToast({
          title: '加载数据中',
          icon: 'loading'
        })
        setTimeout(function () {
          that.getStatus()
          that.getTypes()
        }, 2000)
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  clickPerson: function () {
    var selectPerson = this.data.selectPerson;
    if (selectPerson == true) {
      this.setData({
        selectArea: true,
        selectPerson: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectPerson: true,
      })
    }
  },
  //点击切换
  mySelect: function (e) {
    this.setData({
      content: e.target.dataset.me,
      type_id: e.target.dataset.type_id,
      selectPerson: true,
      selectArea: false,
    })
    if (this.data.content.length != 0) {    
      this.setData({
        num9: 1
      })
    }else{
      this.setData({
        num9: 0
      })
    }
  },
  chooseImage: function (event) {
      var id = event.target.id;
      console.log(id);
      var that = this;
      wx.chooseImage({
        count: 1, // 一次最多可以选择2张图片一起上传
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // var imgeList = that.data.imageList.concat
          //   (res.tempFilePaths);
          // var src = that.data.srcs;
          // src[id] = imgeList;
          // that.src = src;
        var imgs=res.tempFilePaths;
        wx.showToast({  
            title: '正在上传...',  
            icon: 'loading',  
            mask: true,  
            duration: 3000  
        })  
        wx.uploadFile({
          url: app.d.ceshiUrl + '/Api/Public/up_images',  
          filePath: imgs[0],  
          name: 'file',  
          formData: {  
            'uid': app.d.userId,  
          },  
          header: {  
            "Content-Type": "multipart/form-data"  
          },  
          success: function (res) { 
           var data = JSON.parse(res.data);
            console.log(res.data);
            console.log(data.status);
            if(data.status==1){
                 if (id == 0) {
                    that.setData({
                      flag0: true,
                      'srcs[0]': data.msg,
                    })
                    console.log('id=0:' + that.data.srcs)
                    console.log('id=0:' + that.data.srcs.length)
                  } else if (id == 1) {
                    that.setData({
                      flag1: true,
                      'srcs[1]': data.msg,
                    })
                    console.log('id=1:' + that.data.srcs)
                    console.log('id=1:' + that.data.srcs.length)
                  } else if (id == 2) {
                    that.setData({
                      flag2: true,
                      'srcs[2]': data.msg,
                    })
                    console.log('id=2:' + that.data.srcs)
                    console.log('id=2:' + that.data.srcs.length)
                  } else if (id == 3) {
                    that.setData({
                      flag3: true,
                      'srcs[3]': data.msg,
                    })
                    console.log('id=3:' + that.data.srcs)
                    console.log('id=3:' + that.data.srcs.length)
                  } else if (id == 4) {
                    that.setData({
                      flag4: true,
                      'srcs[4]': data.msg,
                    })
                    console.log('id=3:' + that.data.srcs)
                    console.log('id=3:' + that.data.srcs.length)
                  }
            }else{
               wx.showToast({  
                title: data.msg,  
                icon: 'none',  
                duration: 3000  
                })  
            }
          },  
          fail: function (res) {  
            wx.hideToast();  
            wx.showModal({  
              title: '错误提示',  
              content: '上传图片失败',  
              showCancel: false,   
            })  
          }  
        });  
        }
      })
  },
  previewImage: function (e) {
    var dataid = e.target.id;
    wx.previewImage({
      urls: [this.data.srcs[dataid]]
    });
  },
  introduce: function (e) {
    if (e.detail.value.length === 0) {
      wx.showToast({
        title: '介绍不能为空！',
        icon: 'none'
      })
    } else {
      this.setData({
        introduce:e.detail.value
      })
    }
  },
  accountInput:function(e){
    var that=this
      that.setData({
        username:e.detail.value
      })
       wx.request({
        url: app.d.ceshiUrl + '/Api/BIndex/sellAdminCheck',
        method: 'post',
        data: {
          username: that.data.username,
          uid:app.d.userId
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var check = res.data;
          if (check.status == 0) {
            that.setData({
                sellAdmin:0
            })
          }else{
            that.setData({
                sellAdmin:1
            })
          }
        },
        fail: function (e) {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        },
      })
  },
  formSubmit: function (e){
    var that=this
    if (app.globalData.userInfo == null){
      wx.showModal({
        title: '请先登录',
        content: '登录获取更多信息',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../company_user/company_user',
            })
          }
        }
      })
    }else{
        if(e.detail.value.code.length===0){
           wx.showToast({
              title: '服务商码不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(e.detail.value.self_name.length===0){
           wx.showToast({
              title: '真实姓名不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(e.detail.value.card_id.length===0){
           wx.showToast({
              title: '身份证不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if (!/^\d{17}(\d|X|x)$/.test(e.detail.value.card_id)) {
            wx.showToast({
              title: '身份证长度或格式错误！',
              icon: 'none'
            })
            return false;
          }
        if(e.detail.value.tel_id.length===0){
           wx.showToast({
              title: '手机号码不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.tel_id))) {
            wx.showToast({
              title: '手机号格式不正确！',
              icon: 'none'
            })
            return false;
          } 
        if(e.detail.value.store_name.length===0){
           wx.showToast({
              title: '店铺名称不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(e.detail.value.user.length===0){
           wx.showToast({
              title: '用户名不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if (!(/^[a-zA-Z]{1}[\w\.]{5,14}$/.test(e.detail.value.user))) {
            wx.showToast({
              title: '账号首位须为字母，且不少于6位，不大于15位！',
              icon: 'none'
            })
            return false;
        } 
      if(!that.data.sellAdmin){
           wx.showToast({
              title: '登录账号重复',
              duration: 2000,
              icon:'none'
           });
           return false;
       }
      if(e.detail.value.pwd.length===0){
           wx.showToast({
              title: '密码不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if (e.detail.value.pwd.length<6) {
            wx.showToast({
              title: '登录密码不能少于6位！',
              icon: 'none'
            })
            return false;
          }
        if(that.data.region[0]==""||that.data.region[1]==""||that.data.region[2]==""){
           wx.showToast({
              title: '地址不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(e.detail.value.place_desc.length===0){
           wx.showToast({
              title: '详细地址不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(that.data.type_id==0){
           wx.showToast({
              title: '产品分类不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(that.data.flag0==false||that.data.flag1==false||that.data.flag2==false||that.data.flag3==false||that.data.flag4==false){
           wx.showToast({
              title: '请上传个人信息图片',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        if(that.data.introduce==0){
           wx.showToast({
              title: '介绍不能为空',
              duration: 2000,
              icon:'none'
           });
           return false;
        }
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
        console.log(that.data.region[0],
          that.data.region[1],
          that.data.region[2],)
      wx.request({
        url: app.d.ceshiUrl + '/Api/BIndex/sellerAdd',
        method: 'post',
        data: {
            code_one: Math.random(10000, 99999),
            code: e.detail.value.code,
            self_name: e.detail.value.self_name,
            card_id: e.detail.value.card_id,
            tel_id: e.detail.value.tel_id,
            user: e.detail.value.user,
            pwd: e.detail.value.pwd,
            sheng: that.data.region[0],
            city: that.data.region[1],
            quyu: that.data.region[2],
            code: e.detail.value.code,
            place_desc: e.detail.value.place_desc,
            introduce: e.detail.value.introduce,
            type_id: that.data.type_id,
            srcs:that.data.srcs,
            uid: app.d.userId,
            store_name:e.detail.value.store_name,
            id:that.data.seller.id
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data; 
          if(data.status){
            wx.showToast({
              title: '提交成功',
              success: function () {
                wx.reLaunch({
                  url: '../recruitment/recruitment',
                })
              }
            })
          }else{
             wx.showToast({  
                title: data.err,  
                icon: 'none',  
                duration: 3000  
                })  
          }
        }
      })
    } 
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    if (this.data.region.length != 0) {
      this.setData({
        num7: 1
      })
    }else{
      this.setData({
        num7: 0
      })
    }
  },
  modify:function(e){
    var that=this
      that.setData({
          switch2: true,
          switch1: false,
          switch3: true,
      })
  },
  fenxiang: function() {
     var that =this;
     wx.request({
        url: app.d.ceshiUrl + '/Api/Bshare/index',
        method: 'post',
        data: {
          uid: app.d.userId,
          store_id: that.data.store.id,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          var data = res.data;
          that.setData({
            shareimg:data.img
          });
          // wx.downloadFile({
          //   url: data.img,
          //   success(res) {
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              // if (res.statusCode === 200) {
              //     that.setData({
              //          shareimg:res.tempFilePath,
              //      })
              //     that.drawPic();
              // }
            // }
         // })
          
        },
        fail: function() {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        }
      });

  },
    /**
   * 绘制视图
   */
  drawPic(){
  var that = this
    let screenWidth = wx.getSystemInfoSync().windowWidth

      ctx.drawImage(that.data.modelimg, 0, 0, screenWidth*0.9, screenWidth * 0.9 * 1.52)
      ctx.save()
      ctx.beginPath()
      ctx.arc(screenWidth*0.64, screenWidth*0.91, screenWidth*0.23, 0, 2 * Math.PI)
      ctx.closePath()
      ctx.clip()
      ctx.drawImage(that.data.shareimg, screenWidth*0.41, screenWidth*0.683, screenWidth*0.47, screenWidth*0.47)
      ctx.restore()

      ctx.setTextAlign('left')
      ctx.setFillStyle("#242424")
      ctx.setStrokeStyle('#242424')
      ctx.setFontSize(20)
      if(that.data.store.name){
           ctx.fillText(that.data.store.name, screenWidth*0.1, screenWidth*0.15)
      }

      ctx.draw()
  },
  initNavHeight: function() { ////获取系统信息
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth*0.9,
          winHeight: res.windowHeight/4,
          canHeight:res.windowWidth*0.9*1.52,
          winWidth1:res.windowWidth
        });
      }
    });
  },
  zanw:function(){
     wx.showToast({
            title: '暂未开通',
            duration: 2000,
            icon:'none'
     });
  }
})